#!/usr/bin/make -f

# Copyright (c) 2015, 2024, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is designed to work with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms, as
# designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have either included with
# the program or referenced in the documentation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

%:
	dh $@

export DH_VERBOSE=1
export CFLAGS=
export CXXFLAGS=

export LIB_DIR = @LIB_DIR@
export PLUGIN_DIR = $(LIB_DIR)/mysql/libmysqlcppconn@JDBC_ABI_VERSION_MAJOR@/plugin

export CMAKE_OPTS= \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DINSTALL_LIB_DIR=.$(LIB_DIR) \
		-DINSTALL_INCLUDE_DIR=./usr/include \
		-DINSTALL_DOC_DIR=./usr \
		-DWITH_JDBC=ON \
		-DMYSQLCLIENT_STATIC_LINKING=ON \
		-DDEFAULT_PLUGIN_DIR=$(PLUGIN_DIR) \
		-DWITH_SSL=system \
		-DBUNDLE_DEPENDENCIES=ON \
		@DEB_CMAKE_EXTRAS@

override_dh_auto_configure:
	@echo "RULES.$@"
	@echo "cmake options: $(CMAKE_OPTS)"

	rm -rf release && mkdir -p release
	cmake -Brelease -S. $(CMAKE_OPTS)

	rm -rf static && mkdir -p static
	cmake -Bstatic -S. $(CMAKE_OPTS) -DBUILD_STATIC=ON

	# touch $@


override_dh_auto_build:
	@echo "RULES.$@"
	cmake --build release --verbose
	cmake --build static --verbose
	# touch $@


# Note: Disable testing
override_dh_auto_test:

@DEB_RULES_STRIP@

override_dh_auto_install:
	@echo "RULES.$@"

	# complete install first
	cmake --install static --prefix ./debian/tmp
	cmake --install release --prefix ./debian/tmp

	install README.txt debian/tmp/usr/README.txt
	install LICENSE.txt debian/tmp/usr/LICENSE.txt

	install -d -m 0755 debian/tmp/.$(LIB_DIR)/cmake
	install -d -m 0755 debian/tmp/.$(LIB_DIR)/cmake/mysql-concpp
	install debian/tmp/mysql-concpp-config.cmake debian/tmp/.$(LIB_DIR)/cmake/mysql-concpp/mysql-concpp-config.cmake
	install debian/tmp/mysql-concpp-config-version.cmake debian/tmp/.$(LIB_DIR)/cmake/mysql-concpp/mysql-concpp-config-version.cmake
	rm debian/tmp/mysql-concpp-config.cmake
	rm debian/tmp/mysql-concpp-config-version.cmake

	install -d -m 0755 debian/tmp/.$(PLUGIN_DIR)
	mv -v debian/tmp/.$(LIB_DIR)/plugin/* debian/tmp/.$(PLUGIN_DIR)/
	# Note: Remove custom RPATH in plugins -- we want them to use OS-wide dependencies
	chrpath --delete debian/tmp/.$(PLUGIN_DIR)/*
	rm -rf debian/tmp/.$(LIB_DIR)/plugin
	rm -rf debian/tmp/.$(LIB_DIR)/private
	# touch $@

override_dh_installinit:
	@echo "RULES.$@"
	touch $@

override_dh_install:
	dh_install --fail-missing


# Note: Exclude plugins from calculating package dependencies

override_dh_shlibdeps:
	dh_shlibdeps -Xmysql_native_password -Xauthentication_
